package com.mdd.maduoduo.activity;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.gzuliyujiang.wheelpicker.TimePicker;
import com.github.gzuliyujiang.wheelpicker.contract.OnTimePickedListener;
import com.mdd.maduoduo.R;
import com.mdd.maduoduo.di.component.DaggerUIComponent;
import com.mdd.maduoduo.di.component.UIComponent;
import com.mdd.maduoduo.entity.Response;
import com.mdd.maduoduo.presenter.LoginPresenter;
import com.mdd.maduoduo.utils.ToastUtils;
import com.mdd.maduoduo.view.interfaces.UpdateTimeView;

import javax.inject.Inject;

public class SelectTimeActivity extends BaseActivity implements UpdateTimeView {

    private ImageView ivBack;

    private LinearLayout llSelect;

    private TextView tvTime;

    private TextView tvNow;

    private TextView tvSure;

    private String scheduleTime;


    @Inject
    public LoginPresenter mPresenter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_select_time;
    }

    @Override
    protected void initializeInjector() {
        super.initializeInjector();
        mPresenter = new LoginPresenter();
        UIComponent uiComponent = DaggerUIComponent.builder().applicationComponent(getApplicationComponent())
                .activityModule(getActivityModule()).build();
        uiComponent.inject(this);
        mPresenter.setUpdateTimeView(this);
    }

    @Override
    protected void initView() {
        super.initView();
        ivBack = findViewById(R.id.ivBack);
        llSelect = findViewById(R.id.llSelect);
        tvTime = findViewById(R.id.tvTime);
        tvNow = findViewById(R.id.tvNow);
        tvSure = findViewById(R.id.tvSure);

        tvNow.setText(getIntent().getStringExtra("scheduleTime"));
    }

    @Override
    protected void onClick() {
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        llSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showSelect();
            }
        });

        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (scheduleTime!=null&&!scheduleTime.isEmpty()){
                    mPresenter.updateTime(SelectTimeActivity.this,scheduleTime);
                }else {
                    ToastUtils.showLongToast(SelectTimeActivity.this,"请选择通知时间");
                }
            }
        });
    }

    private void showSelect() {
        TimePicker picker = new TimePicker(this);
        picker.setOnTimePickedListener(new OnTimePickedListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onTimePicked(int hour, int minute, int second) {
                if (minute<10){
                    tvTime.setText(hour + ":0" + minute);
                    scheduleTime = hour + ":0" + minute;
                }else {
                    tvTime.setText(hour + ":" + minute);
                    scheduleTime = hour + ":" + minute;
                }

            }
        });
        picker.show();
    }

    @Override
    public void renderSuccess(Response response) {
        ToastUtils.showLongToast(SelectTimeActivity.this,"设置成功");
        finish();
    }
}
